package cn.jhc.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Role;
import cn.jhc.bean.RoleRight;
import cn.jhc.bean.User;
import cn.jhc.dao.DaoType;
import cn.jhc.dao.RoleDAO;
import cn.jhc.dao.RoleRightDAO;
import cn.jhc.dao.UserDao;
import cn.jhc.factory.DAOFactory;

@WebServlet("/addrole.do")
public class AddRoleServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final static Logger logger = Logger.getLogger(AddRoleServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserDao userDao = (UserDao)DAOFactory.getDao(DaoType.USER);
		RoleDAO roleDAO = (RoleDAO)DAOFactory.getDao(DaoType.ROLE);
		List<User> users = userDao.getAll();
		List<Role> roles = roleDAO.getGroups();
		req.setAttribute("roles", roles);
		req.setAttribute("users", users);
	    req.getRequestDispatcher("/WEB-INF/jsp/addrole.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String loginname = req.getParameter("username");
		int roleid = Integer.parseInt(req.getParameter("RoleId"));
		UserDao userDao = (UserDao)DAOFactory.getDao(DaoType.USER);
		RoleRightDAO rightDAO =(RoleRightDAO)DAOFactory.getDao(DaoType.ROLERIGHT);
		int userid = userDao.findIdByUsername(loginname);
		rightDAO.addRoleRight(new RoleRight(roleid, userid));
		logger.info("传进来的名字为："+loginname);
		logger.info("传进来的id为"+roleid);
		logger.info("查询出来的userid为："+roleid);
		
	}
	

}
